﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Collections;

namespace MSL.Utility.Text
{
    /// <summary>
    /// 正则表达式辅助类
    /// </summary>
    public partial class CRegex
    {
        #region 是否匹配

        /// <summary>
        /// 内容是否匹配指定的表达式
        /// </summary>
        /// <param name="input">输入内容</param>
        /// <param name="pattern">表达式字符串</param>
        /// <returns></returns>
        public static bool IsMatch(string input, string pattern)
        {
            if (pattern == "")
                return true;

            return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);// | RegexOptions.IgnorePatternWhitespace);
        }
        #endregion

        #region 多个匹配内容
        /// <summary>
        /// 多个匹配内容
        /// </summary>
        /// <param name="input">输入内容</param>
        /// <param name="pattern">表达式字符串</param>
        /// <param name="groupIndex">第几个分组, 从1开始, 0代表不分组</param>
        public static List<string> GetList(string input, string pattern, int groupIndex)
        {
            List<string> list = new List<string>();
            Regex re = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
            MatchCollection mcs = re.Matches(input);
            foreach (Match mc in mcs)
            {
                if (groupIndex > 0)
                {
                    list.Add(mc.Groups[groupIndex].Value);
                }
                else
                {
                    list.Add(mc.Value);
                }
            }
            return list;
        }

        /// <summary>
        /// 多个匹配内容
        /// </summary>
        /// <param name="input">输入内容</param>
        /// <param name="pattern">表达式字符串</param>
        /// <param name="groupName">分组名, ""代表不分组</param>
        public static List<string> GetList(string input, string pattern, string groupName)
        {
            List<string> list = new List<string>();
            Regex re = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);//| RegexOptions.IgnorePatternWhitespace
            MatchCollection mcs = re.Matches(input);
            foreach (Match mc in mcs)
            {
                if (groupName != "")
                {
                    list.Add(mc.Groups[groupName].Value);
                }
                else
                {
                    list.Add(mc.Value);
                }
            }
            return list;
        }

        /// <summary>
        /// 多个匹配内容 2013-08-26 mashanlin 添加
        /// </summary>
        /// <param name="pattern">正则表达式</param>
        /// <param name="input">内容</param>
        /// <returns></returns>
        public static string GetText(string pattern, string input)
        {
            if (String.IsNullOrEmpty(pattern) || String.IsNullOrEmpty(input))
            {
                return "";
            }
            string tempStr = string.Empty;
            MatchCollection mc = Regex.Matches(input, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);
            foreach (Match m in mc)
            {
                tempStr += m.Value;
            }
            return tempStr;
        }
        /// <summary>
        /// 多个匹配内容   2013-08-26 mashanlin 添加
        /// </summary>
        /// <param name="pattern">正则表达式</param>
        /// <param name="groupNameArr">分组名</param>
        /// <param name="content">内容</param>
        /// <returns></returns>
        public static List<string[]> GetList(string pattern, string[] groupNameArr, string content)
        {
            if (String.IsNullOrEmpty(pattern) || String.IsNullOrEmpty(content) || groupNameArr.Length == 0)
            {
                return null;
            }
            List<string[]> listGroup = new List<string[]>();
            string[] group = new string[groupNameArr.Length];
            MatchCollection mc = Regex.Matches(content, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            foreach (Match m in mc)
            {
                group = new string[groupNameArr.Length];
                for (int i = 0; i < groupNameArr.Length; i++)
                {
                    group[i] = m.Groups[groupNameArr[i]].Value;
                }
                if (listGroup.Contains(group)) continue;
                listGroup.Add(group);
            }
            return listGroup;
        }
        #endregion

        #region 单个匹配内容

        /// <summary>
        /// 单个匹配内容
        /// </summary>
        /// <param name="input">输入内容</param>
        /// <param name="pattern">表达式字符串</param>
        /// <param name="groupIndex">分组序号, 从1开始, 0不分组</param>
        public static string GetText(string input, string pattern, int groupIndex)
        {
            Regex re = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
            Match mc = re.Match(input);
            string result = "";
            if (mc.Success)
            {
                if (groupIndex > 0)
                {
                    result = mc.Groups[groupIndex].Value;
                }
                else
                {
                    result = mc.Value;
                }
            }
            return result;
        }

        /// <summary>
        /// 单个匹配内容
        /// </summary>
        /// <param name="input">输入内容</param>
        /// <param name="pattern">表达式字符串</param>
        /// <param name="groupName">分组名, ""代表不分组</param>
        public static string GetText(string input, string pattern, string groupName)
        {
            Regex re = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
            Match mc = re.Match(input);
            string result = "";
            if (mc.Success)
            {
                if (groupName != "")
                {
                    result = mc.Groups[groupName].Value;
                }
                else
                {
                    result = mc.Value;
                }
            }
            return result;
        }

        #endregion

        #region 替换指定内容

        /// <summary>
        /// 替换指定内容
        /// </summary>
        /// <param name="input">输入内容</param>
        /// <param name="pattern">表达式字符串</param>
        /// <param name="replaceValue">替换值</param>
        /// <param name="groupIndex">分组序号, 0代表不分组</param>
        public static string Replace(string input, string pattern, string replaceValue, int groupIndex)
        {
            Regex re = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
            MatchCollection mcs = re.Matches(input);
            foreach (Match mc in mcs)
            {
                if (groupIndex > 0)
                {
                    input = input.Replace(mc.Groups[groupIndex].Value, replaceValue);
                }
                else
                {
                    input = input.Replace(mc.Value, replaceValue);
                }
            }
            return input;
        }
        /// <summary>
        /// 替换匹配的分组内容
        /// </summary>
        /// <param name="input">输入值</param>
        /// <param name="pattern">正则</param>
        /// <param name="replacement">使用指定的字符串替换匹配内容，若为空则替换成空字符串</param>
        /// 如：将“”日期：03/09/2014” 字符串中的日期替换成{yyyy-MM-dd}的样式，应该这样写：(注：${year}意思为分组名为year)
        /// ①Utility.Text.CRegex.Replace(input, @"(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})", "${year}-${month}-${day}");
        /// ②Utility.Text.CRegex.Replace(input, @"(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})", "$3-$1-$2");
        /// <returns></returns>
        public static string Replace(string input, string pattern, string replacement)
        {
            input = Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
            return input;
        }
        /// <summary>
        /// 根据正则去除多余字符
        /// </summary>
        /// <param name="input">源码</param>
        /// <param name="pattern">正则</param>
        /// <returns></returns>
        public static string Remove(string input, string pattern)
        {
            return Replace(input, pattern, "");
        }
        #endregion

        #region 分组
        /// <summary>
        /// 
        /// </summary>
        /// <param name="text">字符串</param>
        /// <param name="splitStr">分割符</param>
        /// <returns></returns>
        public static string[] Split(string text, string splitStr)
        {
            Regex reg = new Regex(splitStr);
            return reg.Split(text);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="text">字符串</param>
        /// <param name="splitStr">分割符</param>
        /// <param name="option">正则枚举值</param>
        /// <returns></returns>
        public static string[] Split(string text, string splitStr, RegexOptions option)
        {
            Regex reg = new Regex(splitStr, option);
            return reg.Split(text);
        }
        #endregion

        #region 获取一条SQL语句中的所参数

        /// <summary>
        /// 获取一条SQL语句中的所参数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns></returns>
        public static List<String> SqlParame(string sql)
        {
            List<String> list = new List<String>();
            Regex r = new Regex(@"@(?<x>[0-9a-zA-Z]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            MatchCollection mc = r.Matches(sql);
            for (int i = 0; i < mc.Count; i++)
            {
                list.Add(mc[i].Result("$1"));
            }
            return list;
        }
        /// <summary>
        /// 获取一条SQL语句中的所参数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns></returns>
        public static List<String> OracleParame(string sql)
        {
            List<String> list = new List<String>();
            Regex r = new Regex(@":(?<x>[0-9a-zA-Z]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            MatchCollection mc = r.Matches(sql);
            for (int i = 0; i < mc.Count; i++)
            {
                list.Add(mc[i].Result("$1"));
            }

            return list;
        }
        #endregion
    }
}
